package com.swpu.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swpu.project.common.BaseResponse;
import com.swpu.project.mapper.DataDictionaryMapper;
import com.swpu.project.mapper.DictionaryContentMapper;
import com.swpu.project.model.dto.data.DictionaryDto;
import com.swpu.project.model.entity.DataDictionary;
import com.swpu.project.model.entity.DictionaryContent;
import com.swpu.project.service.IDataDictionaryService;
import com.swpu.project.service.IDictionaryContentService;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author swpuyanxiegroup1
 * @since 2024-07-30
 */
@Service
@NoArgsConstructor
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements IDataDictionaryService {


   @Resource
    DictionaryContentMapper dictionaryContentMapper;
@Resource
    IDictionaryContentService iDictionaryContentService;

    @Override
    public BaseResponse queryDict() {
        List<DictionaryDto> dictionaryDtos = new ArrayList<>();
        List<DataDictionary> dataDictionaryList = lambdaQuery().list();

       dataDictionaryList.get(0);

        for (int i = 0; i < dataDictionaryList.size(); i++) {
            DictionaryDto dictionaryDto = new DictionaryDto();
            DataDictionary dataDictionary = dataDictionaryList.get(i);
            dictionaryDto.setDataDictionary(dataDictionary);
            dictionaryDtos.add(dictionaryDto);

        }
        List<DictionaryContent> list1 = iDictionaryContentService.list();
        Map<String, DictionaryDto> collect1 = dictionaryDtos.stream().
                collect(Collectors.toMap(dictionaryDto -> dictionaryDto.getDataDictionary().getDictId(),
                dictionaryDto -> dictionaryDto));
        HashMap<String, List<DictionaryContent>> stringListHashMap = new HashMap<>();
        for (DictionaryContent dictionaryContent : list1) {
            String dictId = dictionaryContent.getDictId();
            if (stringListHashMap.containsKey(dictId)) {
                stringListHashMap.get(dictId).add(dictionaryContent);
                continue;
            }
            ArrayList<DictionaryContent> dictionaryContents = new ArrayList<>();
               dictionaryContents.add(dictionaryContent);
            stringListHashMap.put(dictId,dictionaryContents);

        }

        for (Map.Entry<String, DictionaryDto> stringDictionaryDtoEntry : collect1.entrySet())
        {
            String key = stringDictionaryDtoEntry.getKey();
            DictionaryDto value = stringDictionaryDtoEntry.getValue();
            List<DictionaryContent> dictionaryContents = stringListHashMap.get(key);
            value.setDictionaryContents(dictionaryContents);


        }
        Collection<DictionaryDto> values = collect1.values();

        return BaseResponse.ok(values);
    }




}
